package com.app.business.web.business.newTextForCs;

import com.app.business.model.House;
import com.app.business.model.newTextForCs.TextHistory;
import com.app.business.model.newTextForCs.TextHouseHoldAndArea;
import com.app.business.model.newTextForCs.TextParameter;
import com.app.business.service.newTextForCs.TextContractService;
import com.app.business.service.newTextForCs.TextHistoryService;
import com.app.business.service.newTextForCs.TextHouseHoldAndAreaService;
import com.app.business.service.newTextForCs.TextParameterService;
import com.app.system.jpa.PageBean;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 测算基础信息控制层
 * Created by ddz on 2020/1/20.
 */
@Controller
@RequestMapping("/business/newTextForCs/textHistory/*")
@SuppressWarnings("all")
public class TextHistoryController {
    @Autowired
    private TextParameterService textParameterService;
    @Autowired
    private TextHouseHoldAndAreaService textHouseHoldAndAreaService;
    @Autowired
    private TextHistoryService textHistoryService;
    @Autowired
    private TextContractService textContractService;
    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private Configuration freemarkerConfiguration; // 注入bean，这里是属性注入，推荐构造注入

    /**
     * 测算结果数据管理列表
     */
    @RequestMapping("textHistoryList")
    @RequiresPermissions("textHistory:textHistoryList")
    public void textHistoryList() {
    }

    /**
     * 加载基础信息数据
     *
     * @return
     */
    @RequestMapping("loadTextHistoryList")
    @ResponseBody
    @RequiresPermissions("textHistory:loadTextHistoryList")
    public Map loadTextHistoryList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<TextHistory> textHistoryPageBean = this.textHistoryService.getTextHistoryPageBean();
        List<TextHistory> textHistoryList = textHistoryPageBean.getDatas();


        if (CollectionUtils.isNotEmpty(textHistoryList)) {
            Map<String, Object> item = null;
            for (TextHistory textHistory : textHistoryList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", textHistory.getId());
                item.put("projectCode", StringUtils.getNotNull(textHistory.getTextContract().getProjectCode()));
                item.put("projectName", StringUtils.getNotNull(textHistory.getTextContract().getProjectName()));
                item.put("parameterName", StringUtils.getNotNull(textHistory.getTextParameter().getParameterName()));
                item.put("contractCode", StringUtils.getNotNull(textHistory.getTextContract().getContractCode()));
                item.put("name", StringUtils.getNotNull(textHistory.getTextContract().getName()));
                item.put("chooseNum", StringUtils.getNotNull(textHistory.getTextContract().getChooseNum()));
                item.put("kazArea", BigDecimalUtils.getNotNullStr(textHistory.getTextContract().getKazArea()));
                item.put("name1", StringUtils.getNotNull(textHistory.getName1()));
                item.put("count1", IntegerUtils.getNotNullStr(textHistory.getCount1()));
                item.put("name2", StringUtils.getNotNull(textHistory.getName2()));
                item.put("count2", IntegerUtils.getNotNullStr(textHistory.getCount2()));
                item.put("name3", StringUtils.getNotNull(textHistory.getName3()));
                item.put("count3", IntegerUtils.getNotNullStr(textHistory.getCount3()));
                item.put("name4", StringUtils.getNotNull(textHistory.getName4()));
                item.put("count4", IntegerUtils.getNotNullStr(textHistory.getCount4()));
                item.put("name5", StringUtils.getNotNull(textHistory.getName3()));
                item.put("count5", IntegerUtils.getNotNullStr(textHistory.getCount5()));


                listData.add(item);
            }
        }
        return LayUiUtils.page(textHistoryPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 根据筛选条件获取记录
     */
    @RequestMapping("textHistoryListExcel")
    @RequiresPermissions("textHistory:textHistoryListExcel")
    public void textHistoryListExcel(HttpServletRequest request, HttpServletResponse response) {
        PageBean<TextHistory> textHistoryPageBean = this.textHistoryService.getTextHistoryPageBean();
        List<TextHistory> textHistoryList = textHistoryPageBean.getDatas();

        try {
            // 封装导出数据
            Map data = Maps.newHashMap();
            data.put("textHistoryList", textHistoryList);

            data.put("exportPerson", SecurityUtils.getSubject().getPrincipal());   //导出人姓名
            data.put("exportTime", DateUtils.dateToStringFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));   //导出时间
//            data.put("decisions", exportDecisionList); //第二个worksheet的数据对象
            request.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(("测算数据明细导出.xls").getBytes(), "iso-8859-1"));
            freemarkerConfiguration.getTemplate("/business/newTextForCs/textHistory/textHistoryListExcel.ftl").process(data, response.getWriter());
        } catch (Exception e) {
            logger.error("文件下载异常", e);
            e.printStackTrace();
        }
    }


    /**
     * 数据配置表单
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("textHistoryForm")
    @RequiresPermissions("textHistory:textHistoryForm")
    public void textHistoryForm(HttpServletRequest request, ModelMap modelMap) {

    }

//    /**
//     * 数据保存执行
//     *
//     * @param request
//     * @return
//     * @throws AjaxException
//     */
//    @RequestMapping("textHistoryFormSave")
//    @ResponseBody
//    @RequiresPermissions("textHistory:textHistoryFormSave")
//    public synchronized Response dataFormSave(HttpServletRequest request) throws AjaxException {
//        Response response = this.textHistoryService.dataFormSave(request);
//        return response;
//    }


    /**
     * 测算情况汇总表
     */
    @RequestMapping("historyStatistics")
    @RequiresPermissions("textHistory:historyStatistics")
    public void historyStatistics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        String textParameterCode = ParamUtils.getString(request, "textParameterCode", "");
        String projectCode = ParamUtils.getString(request, "projectCode", "");
        TextParameter textParameter = this.textParameterService.getTextParameterByParameterCode(textParameterCode);
        this.textHistoryService.dataFormSave(textParameter, projectCode)
        ;
        List<String> projectNameList = this.textContractService.getTextContractProjectNameList(projectCode);
        List<TextHouseHoldAndArea> houseHoldAndAreaList = this.textHouseHoldAndAreaService.getTextHouseHoldAndAreaListByTextParameterId(textParameter.getId());
        Map<String, Map> countNumMap = this.textHistoryService.getCountStatisticsByAzProjectCode(projectCode, textParameter.getId());

        modelMap.addAttribute("projectNameList", projectNameList);
        modelMap.addAttribute("houseHoldAndAreaList", houseHoldAndAreaList);
        modelMap.addAttribute("countNumMap", countNumMap);
        modelMap.addAttribute("bigUtils", new BigDecimalUtils());

    }

}
